'use strict';

const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Field = app.model.define('field', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING(30),
      allowNull: false,
      unique: true,
      comment: '专业领域名称',
    },
    college_id: {
      type: INTEGER,
      allowNull: false,
      comment: '所属学院ID',
    },
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    }
  }, {
    timestamps: true,
    tableName: 'field',
    comment: '专业领域表',
    createdAt: 'create_time',
    updatedAt: 'updated_time'
  });

  Field.associate = function() {
    app.model.Field.belongsTo(app.model.College, { foreignKey: 'college_id' });
  };

  return Field;
};

// 根据以上字段生成mysql建表语句
// CREATE TABLE `field` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `name` varchar(255) DEFAULT NULL,
//   `college_id` int(11) DEFAULT NULL,
//   `create_time` datetime DEFAULT NULL,
//   `updated_time` datetime DEFAULT NULL,
//   PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
